package com.jdd.pedicure.service;

import com.jdd.pedicure.dao.ItemDao;
import com.jdd.pedicure.entity.Item;
import com.jdd.pedicure.entity.Result;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class ItemService {
    @Resource
    ItemDao itemDao;

    public Item queryById(int id) {
        return itemDao.getOne(id);
    }

    public List<Item> queryAll() {
        return itemDao.findAll();
    }

    public void delete(Integer id) {
        itemDao.deleteById(id);
    }
    public void deletes(List<Item> items) {
        itemDao.deleteAll(items);
    }
    public void saveOrUpdate(Item item) {
        itemDao.save(item);
    }

    public Result<Item> queryByPage(Result<Item> result) {
        PageRequest request = new PageRequest(result.getPage() - 1, result.getLimit());
        Example<Item> ex = Example.of(result.getExample());
        Page<Item> all = itemDao.findAll(ex, request);
        result.setData(all.getContent());
        result.setCount((int) all.getTotalElements());
        return result;
    }
}
